<?php
namespace Home\Controller;
use Think\Controller;
class UserController extends Controller {
    //前置操作方法
    public function _before_index(){
        $user = is_login();
        if(!$user){
            $this->redirect('Index/index');
        }
    }
    /**
     * 个人中心-个人信息
     */
    public function userlist(){
        $uid = is_login();
        if($uid){
            $M_hospital = M('hospital');
            $hos = $M_hospital->where('id = '.$uid)->field('name,integral')->find();
            $this->assign('hos',$hos);
        }
        $this->display();
    }
    /**
     * 个人中心-个人信息详情
     */
    public function userinfo(){
        $uid = is_login();
        if($uid){
            $M_hospital = M('hospital');
            $hos = $M_hospital->where('id = '.$uid)->find();
            $this->assign('hos',$hos);
        }
        $this->display();
    }
    /**
     * 个人中心-个人信息修改
     */
    public function usermod(){
        $uid = is_login();
        if($uid){
            if(IS_POST){
                if(I('post.pic')){
                    $data['pic']        = I('post.pic')[0];
                }
                $data['content']    = I('post.content');
                //print_r($data);die;
                $M_hospital = M('hospital');
                $hos = $M_hospital->where('id = '.$uid)->save($data);
                if($hos){
                    $return     = array('code'=>0,'msg'=>'修改成功！','url'=>U('User/userlist'));
                    $this->ajaxReturn($return,'json');
                }else{
                    $return     = array('code'=>-1,'msg'=>'修改失败！');
                    $this->ajaxReturn($return,'json');
                }
            }
        }
    }
    /**
     * 个人中心--转诊记录
     */
    public function referrallist(){
        $uid            = is_login();
        $M_category     = M('category');
        $M_state        = M('state');
        $catelist       = $M_category->select();
        $statelist      = $M_state->select();
        $pagesize       = '1';
        if($uid){
            if(IS_POST){
                //$where['ms_referral.hospital_now'] = $uid;

                if(I('post.cateid')){
                    $where['ms_referral.cateid']    = I('post.cateid');
                }
                if(I('post.state')){
                    $where['ms_referral.state']     = I('post.state');
                }
                if(I('post.starttime')){
                    $timestart1  = I('post.starttime').' 00:00:00';
                }
                if(I('post.endtime')){
                    $timeend1    = I('post.endtime').' 23:59:59';
                }
                if($timestart1 && $timeend1){
                    $where['ms_referral.addtime'] = array(array('EGT',$timestart1),array('ELT',$timeend1,'AND'));
                }
                if(I('post.paname')){
                    $name = I('post.paname');
                    $where['ms_referral.pname'] = array('like', "%$name%");
                }
                $page       = I('post.page');
                $start      = ($page-1)*$pagesize;
                $M_referral = M('referral');
                $list = $M_referral ->where($where)
                    ->where("ms_referral.hospital_pre = '".$uid."' or ms_referral.hospital_now = '".$uid."'")
                    ->join('ms_category as c on ms_referral.cateid=c.id')
                    ->join('ms_state as s on ms_referral.state=s.id')
                    ->join('ms_hospital as h on ms_referral.hospital_now=h.id')
                    ->limit($start,$pagesize)
                    ->order('ms_referral.addtime DESC')
                    ->field('ms_referral.id,ms_referral.pname,ms_referral.hospital_pre,h.name as hospital_now_name,ms_referral.addtime,c.title as catename,s.id as stateid,s.title as state,s.title2 as state2')
                    ->select();
                //echo $M_referral->getlastsql();
                if($list){
                    $return_data     = array('code' => 0,'data' => $list,'page'=>$page+1,'uid'=>is_login());
                    $this->ajaxReturn($return_data,'json');
                }else{
                    $return_data     = array('code' => -1,'msg' => '没有更多的记录了');
                    $this->ajaxReturn($return_data,'json');
                }
            }
        }
        $starttime  = date("Y-m-01",time());
        $endtime    = date("Y-m-d",time());

        $this->assign(array(
            'catelist' => $catelist,
            'statelist' => $statelist,
            'starttime' => $starttime,
            'endtime'   => $endtime
        ));
        $this->display();
    }
    /**
     * 个人中心--转诊详情
     */
    public function referral_detail(){
        $id = I('get.id');
        if($id){
            $M_referral = M('referral');
            $M_patient  = M('patient');
            $M_category  = M('category');
            $M_hospital  = M('hospital');
            $M_state  = M('state');
            $where['id']= $id;
            $info       = $M_referral->where($where)->find();
            $patient    = $M_patient->where('id = '.$info['pid'])->field('pic,remarks')->find();
            if(strstr($patient['pic'], ',')){
                $pic = explode(',',$patient['pic']);
            }else{
                $pic = $patient['pic'];
            }
            $info['patient_remarks'] = $patient['remarks'];
            $cate       = $M_category->where(array('id'=>$info['cateid']))->find();
            $hospital   = $M_hospital->where(array('id'=>$info['hospital_now']))->find();
            $state  = $M_state->where(array('id'=>$info['state']))->find();
            $info['cate'] = $cate['title'];
            $info['hospital_now'] = $hospital['name'];
            $info['state1'] = $state['title'];
            $info['state2'] = $state['title2'];
            $this->assign('info',$info);
            $this->assign('pic',$pic);
            $this->assign('uid',is_login());
        }
        $this->display();
    }
    /**
     * 个人中心--转诊状态改变
     */
    public function state(){
        $uid = is_login();
        if($uid){
            $M_referral = M('referral');
            $id         = I('post.id');
            $feedback   = I('post.feedback');
            $stateid    = I('post.state');
            $data['state'] = $stateid;
            $data['feedback'] = $feedback;
            $mod = $M_referral->where(array('id'=>$id))->save($data);
            if($mod){
                $return     = array('code'=>0,'msg'=>'操作成功！','url'=>U('User/referrallist'));
                $this->ajaxReturn($return,'json');
            }else{
                $return     = array('code'=>-1,'msg'=>'操作失败！');
                $this->ajaxReturn($return,'json');
            }

        }
    }
    /**
     * 个人中心--病例记录
     */
    public function patientlist(){
        $uid            = is_login();
        $M_category     = M('category');
        $catelist       = $M_category->select();
        $pagesize	    = '1';
        if($uid){
            if(IS_POST){
                $where['hid'] = $uid;

                if(I('post.cateid')){
                    $where['cateid']       = I('post.cateid');
                }
                if(I('post.starttime')){
                    $timestart1  = I('post.starttime').' 00:00:00';
                }
                if(I('post.endtime')){
                    $timeend1    = I('post.endtime').' 23:59:59';
                }
                if($timestart1 && $timeend1){
                    $where['ms_patient.addtime'] = array(array('EGT',$timestart1),array('ELT',$timeend1,'AND'));
                }
                if(I('post.patientname')){
                    $name = I('post.patientname');
                    $where['name'] = array('like', "%$name%");
                }
                $M_patient  = M('patient');
                $page       = I('post.page');
                $start      = ($page-1)*$pagesize;
                $list = $M_patient  ->where($where)
                                    ->join('ms_category as c on ms_patient.cateid=c.id')
                                    ->order('ms_patient.addtime DESC')
                                    ->limit($start,$pagesize)
                                    ->field('ms_patient.id,ms_patient.name,c.title,ms_patient.addtime')
                                    ->select();
                if($list){
                    $return     = array('code'=>0,'data'=>$list,'page'=>$page+1);
                    $this->ajaxReturn($return,'json');
                }else{
                    $return     = array('code'=>-1,'msg'=>'没有更多患者了！');
                    $this->ajaxReturn($return,'json');
                }
            }
        }
        $starttime  = date("Y-m-01");
        $endtime    = date("Y-m-d");
        $this->assign(array(
            'catelist'  => $catelist,
            'starttime' => $starttime,
            'endtime'   => $endtime
        ));
        $this->display();
    }
    public function ajax_upload(){//异步上传图片的地址
        // 根据自己的业务调整上传路径、允许的格式、文件大小
        ajax_upload('userinfo');
    }
    /*public function webuploader(){//最终提交表单的地址
        if(IS_POST){
            var_dump($_POST);die;
        }else{
            $this->display();
        }
    }*/
    /**
     * 个人中心--病例添加
     */
    public function patientadd(){
        $M_category = M('category');
        $catelist   = $M_category->select();
        if(IS_POST){
            $M_patient          = M('patient');
            $data['name']       = I('post.name');
            $data['hid']        = is_login();
            $data['cateid']     = I('post.cateid');
            $data['pic']        = implode(',',I('post.pic'));
            $data['remarks']    = I('post.remarks');
            $data['addtime']    = date("y-m-d H:i:s",time());
            $add = $M_patient->add($data);
            if($add){
                $return     = array('code'=>0,'msg'=>'添加成功！','url'=>U('Referral/patientlist'));
                $this->ajaxReturn($return,'json');
            }else{
                $return     = array('code'=>-1,'msg'=>'添加失败！');
                $this->ajaxReturn($return,'json');
            }
        }
        $this->assign('catelist',$catelist);
        $this->display();
    }
    /**
     * 个人中心--病例详情
     */
    public function patient_detail(){
        $id = I('get.id');
        if($id){
            $M_patient   = M('patient');
            $M_category  = M('category');
            $M_hospital  = M('hospital');
            $M_referral  = M('referral');
            $M_state     = M('state');
            $where['id']= $id;
            $patient    = $M_patient->where('id = '.$id)->find();
            if(strstr($patient['pic'], ',')){
                $pic = explode(',',$patient['pic']);
            }else{
                $pic = $patient['pic'];
            }
            $cate       = $M_category->where(array('id'=>$patient['cateid']))->find();
            $hospital   = $M_hospital->where(array('id'=>$patient['hid']))->find();
            $referral   = $M_referral->where(array('pid'=>$patient['id']))->find();
            $state      = $M_state->where(array('id'=>$referral['state']))->find();
            $patient['cate'] = $cate['title'];
            $patient['hospital'] = $hospital['name'];
            $patient['statename'] = $state['title'];
            //print_r($patient);print_r($referral);
            $this->assign('pic',$pic);
            $this->assign('info',$patient);
        }
        $this->display();
    }
    /**
     * 个人中心--病例修改
     */
    public function patientmod(){
        $id = I('post.id');
        if($id){
            $M_patient  = M('patient');
            if(I('post.pic')){
                $data['pic'] = implode(',',I('post.pic'));;
                $patient    = $M_patient->where('id = '.$id)->save($data);
                if($patient){
                    $return_data     = array('code' => 0,'msg' => '修改成功','url'=>U('User/patientlist'));
                    $this->ajaxReturn($return_data,'json');
                }else{
                    $return_data     = array('code' => -1,'msg' => '没有更多的记录了');
                    $this->ajaxReturn($return_data,'json');
                }
            }
            $return_data     = array('code' => 0,'msg' => '修改成功','url'=>U('User/patientlist'));
            $this->ajaxReturn($return_data,'json');
        }
    }
    /**
     * 个人中心--病例删除
     */
    public function patientdel(){
        $id = I('post.id');
        if($id){
            $M_patient  = M('patient');
            $patient    = $M_patient->where(array('id'=>$id))->delete();
            if($patient){
                $return_data     = array('code' => 0,'msg' => '删除成功','url'=>U('User/patientlist'));
                $this->ajaxReturn($return_data,'json');
            }else{
                $return_data     = array('code' => -1,'msg' => '删除失败');
                $this->ajaxReturn($return_data,'json');
            }
        }
    }
    /**
     * 修改密码
     */
    public function passmod(){
        $uid = is_login();
        if($uid){
            if(IS_POST){
                $oldpass = I('post.oldpass');
                $data['password'] = md5(I('post.newpass'));
                $M_hospital = M('hospital');
                $is_hos = $M_hospital->where('id = '.$uid)->find();
                if(md5($oldpass) == $is_hos['password']){
                    $hos_save = $M_hospital->where('id = '.$uid)->save($data);
                    if($hos_save){
                        $return     = array('code'=>0,'msg'=>'密码修改成功！','url'=>U('User/userlist'));
                        $this->ajaxReturn($return,'json');
                    }else{
                        $return     = array('code'=>-2,'msg'=>'密码修改失败！');
                        $this->ajaxReturn($return,'json');
                    }
                }else{
                    $return     = array('code'=>-1,'msg'=>'旧密码输入错误');
                    $this->ajaxReturn($return,'json');
                }
            }
        }
        $this->display();
    }
    /**
     * 修改密码
     */
    /*public function editpass() {
        if(IS_POST){
            $oldpass    = I('post.oldpass');
            $newpass1   = I('post.newpass1');
            $newpass2   = I('post.newpass2');
            $c_uid      = cookie('u_uid');
            if(!$c_uid){
                $return = array("status"=>-1,"msg"=>"请先登录！");
                $this->ajaxReturn($return,'JSON');
            }
            $M_user     = M('user');
            $user_row   = $M_user->where(array('id'=>$c_uid))->find(); 
            if($user_row['password'] != md5($oldpass)){
                $return = array("status"=>-1,"msg"=>"旧密码错误!");
                $this->ajaxReturn($return,'JSON');
            }
            if($newpass1 != $newpass2){
                $return = array("status"=>-1,"msg"=>"两次新密码不一致!");
                $this->ajaxReturn($return,'JSON');
            }
            $data['password']   = $newpass1;
            $save = $M_user->where(array('id'=>$c_uid))->save($data);
            if($save){
                $return = array("status"=>0,"msg"=>"密码修改成功!请重新登录");
                $this->ajaxReturn($return,'JSON');
            }else{
                $return = array("status"=>-1,"msg"=>"密码修改失败!");
                $this->ajaxReturn($return,'JSON');
            }
        }else{
            $this->display();
        }
    }*/

    

    /**
     * 获取用户
     */
    private function getUser() {
        $c_phone = cookie('u_phone');
        $c_pass = cookie('u_pass');
        $ck_phone = Util::authcode($c_pass, 'DECODE');
        
        if (!$c_phone OR $c_phone != $ck_phone) {
            cookie('u_phone', null);
            cookie('u_pass', null);
        
            $this->error('请登录后再操作！', U('User/login'));
        }
    
        $M_user = M('user');
    
        $user_row = $M_user->where(array('phone'=>$c_phone))->find();
        
        return $user_row;
    }
}